package tk.uodrad.apex.project.ex.handler;

import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import tk.uodrad.apex.project.ex.ServiceException;
import tk.uodrad.apex.project.web.JsonResult;
import tk.uodrad.apex.project.web.ServiceCode;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.Objects;
import java.util.Set;

/**
 * 说明：
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(ServiceException.class)
    public JsonResult<Void> handleServiceException(ServiceException e) {
        return JsonResult.fail(e);
    }

    @ExceptionHandler
    public JsonResult<Void> handleBindException(BindException e) {
        String message = Objects.requireNonNull(e.getFieldError()).getDefaultMessage();
        return JsonResult.fail(ServiceCode.ERROR_BAD_REQUEST, message);
    }

    @ExceptionHandler
    public JsonResult<Void> handleConstraintViolationException(ConstraintViolationException e) {
        StringBuilder stringBuilder = new StringBuilder();
        Set<ConstraintViolation<?>> constraintViolationExceptions = e.getConstraintViolations();
        for (ConstraintViolation<?> constraintViolation : constraintViolationExceptions) {
            stringBuilder.append(constraintViolation.getMessage());
        }
        String message = stringBuilder.toString();
        return JsonResult.fail(ServiceCode.ERROR_BAD_REQUEST, message);
    }

    @ExceptionHandler
    public JsonResult<Void> handleThrowable(Throwable e) {
        String message = "服务器忙，请稍后再次尝试（开发过程中，如果看到此提示，请检查控制台的信息，并补充处理异常的方法）！";
        e.printStackTrace(); // 注意：在生产环境中，禁止使用此语句！（有性能瓶颈）
        return JsonResult.fail(ServiceCode.ERROR, message);
    }
}
